package com.snc.controller.hxsq.apply;


import com.snc.controller.index.BaseController;
import com.snc.entity.TsFormMap;
import com.snc.exception.SystemException;
import com.snc.plugin.PageView;
import com.snc.plugin.Status;
import com.snc.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 
 * @author lanyuan 2015-11-19
 * @Email: mmm333zzz520@163.com
 * @version 4.0v
 */
@Controller
@RequestMapping("/apply/")
public class TsAdminController extends BaseController {
	
	@RequestMapping("listUI")
	public String listUI(Model model) throws Exception {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/hxsq/apply/list";
	}

	@ResponseBody
	@RequestMapping("findByPage")
	public PageView findByPage( String pageNow,
			String pageSize) throws Exception {
		pageView = getPageView(pageNow,pageSize);
		int limit = (pageView.getPageNow() - 1) * pageView.getPageSize();
		int offset = pageView.getPageSize();

		TsFormMap map = new TsFormMap();
		long count = map.findByAll().size();
		pageView.setRowCount(count);
		map.put("limit",limit);
		map.put("offset",offset);
		List<TsFormMap> list = map.mapper().selectByPage(map);
		pageView.setRecords(list);
        return pageView;
	}

	@RequestMapping("replyUI")
	public String infoUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty(id)){
			model.addAttribute("id", id);
		}
		return Common.BACKGROUND_PATH + "/hxsq/apply/reply";
	}

	@ResponseBody
	@RequestMapping("reply")
	@Transactional(readOnly=false)
	public String reply() throws Exception {
		try {
			TsFormMap applyFormMap = super.toFormMap(TsFormMap.class);
			applyFormMap.put("status","R");
			applyFormMap.update();

		} catch (Exception e) {
			//try catch 必须抛出自定义异常,否则事务失效!   #### lanyuan ####
			String results = "{\"results\":\"error\",\"message\":\""+e+"\"}";
			throw new SystemException(results);
		}
		return "success";
	}

	@RequestMapping("close")
	@ResponseBody
	@Transactional
	public String close(String ids) throws Exception {
		for(String id : ids.split(",")){
			TsFormMap map = new TsFormMap();
			TsFormMap old = map.findById(id);
			if(old.getStr("status").equals("C")){
				map.put("id",id);
				map.put("status", Status.N.toString());
				map.update();
			}
		}
		return "success";
	}

}